package com.jprocms.module.cms.util;

import cn.hutool.http.useragent.UserAgent;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import static com.jprocms.module.cms.constant.CmsSysConstants.COOKIE_PREVIEW;

/**
 * @author jprocms
 */
public class DeviceUtil {

    /**
     * 判断当前请求是否PC端请求
     *
     * @param request
     * @return
     */
    public static boolean isPc() {
        HttpServletRequest request = RequestUtils.getHttpServletRequest();
        Cookie deviceCookie = null;
        if (request != null) {
            deviceCookie = CookieUtils.getCookie(request, COOKIE_PREVIEW);
            if (deviceCookie != null) {
                String deviceVal = deviceCookie.getValue();
                if (StringUtils.isBlank(deviceVal) || deviceVal.equals(COOKIE_PREVIEW)) {
                    return true;
                }
            } else {
                String userAgentStr = request.getHeader("User-Agent");
                UserAgent userAgent = UserAgentUtils.parse(userAgentStr);
                return !userAgent.isMobile();
            }
        }
        return false;
    }
}
